package cn.edu.jju.stepcounter2.user;


import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.TextInputLayout;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.View;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.Toast;

import com.rey.material.widget.RadioButton;
import com.rey.material.widget.Slider;
import com.squareup.leakcanary.RefWatcher;

import org.xutils.common.util.KeyValue;
import org.xutils.db.sqlite.WhereBuilder;
import org.xutils.ex.DbException;
import org.xutils.x;

import cn.edu.jju.stepcounter2.R;
import cn.edu.jju.stepcounter2.SCApp;
import cn.edu.jju.stepcounter2.bean.DayData;
import cn.edu.jju.stepcounter2.chart.ChartActivity;

import cn.edu.jju.stepcounter2.utils.AppManager;
import cn.edu.jju.stepcounter2.utils.GetDate;
import cn.edu.jju.stepcounter2.utils.SPUtils;
import cn.edu.jju.stepcounter2.utils.ShowToastUtil;


public class UserActivity extends AppCompatActivity implements CompoundButton.OnCheckedChangeListener {


    private RadioButton rb_nam;
    private RadioButton rb_woman;
    private EditText et_age;
    private EditText et_height;
    private EditText et_weight;
    private EditText et_expectStep;


    private TextInputLayout til_age;
    private TextInputLayout til_height;
    private TextInputLayout til_weight;
    private TextInputLayout til_expect;
    private String age;
    private String height;
    private String weight;
    private String expectStep;
    private String sex;
    private Slider sli_expectStep;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_user);
        //AppManager.getAppManager().addActivity(this);
        initData();
        initView();


    }

    private void initData() {
        age = (String) SPUtils.get(this, "age", "1");
        height = (String) SPUtils.get(this, "height", "1");
        weight = (String) SPUtils.get(this, "weight", "1");
        expectStep = (String) SPUtils.get(this, "expectStep", "1");
        sex = (String) SPUtils.get(this, "sex", "1");

    }

    private void initView() {
        til_age = (TextInputLayout) findViewById(R.id.til_age);
        til_height = (TextInputLayout) findViewById(R.id.til_height);
        til_weight = (TextInputLayout) findViewById(R.id.til_weight);
        til_expect = (TextInputLayout) findViewById(R.id.til_expectStep);
        rb_nam = (RadioButton) findViewById(R.id.rb_man);
        rb_woman = (RadioButton) findViewById(R.id.rb_woman);
        et_age = til_age.getEditText();
        et_height = til_height.getEditText();
        et_weight = til_weight.getEditText();
        et_expectStep = til_expect.getEditText();

        if ("1".equals(sex)) {
            rb_nam.setChecked(true);
            rb_woman.setChecked(false);
        } else {
            rb_woman.setChecked(true);
            rb_nam.setChecked(false);
        }
        et_age.setText(age);
        et_expectStep.setText(expectStep);
        et_height.setText(height);
        et_weight.setText(weight);


        rb_nam.setOnCheckedChangeListener(this);
        rb_woman.setOnCheckedChangeListener(this);
    }

    /**
     * @param compoundButton 所点击radiobutton
     * @param b
     */
    @Override
    public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
        if (b) {
            rb_nam.setChecked(rb_nam == compoundButton);
            rb_woman.setChecked(rb_woman == compoundButton);
        }


    }

    public void save(View view) {
        saveData();
    }

    private void saveData() {
        String age = Integer.parseInt(et_age.getText().toString().trim()) + "";
        String expectStep = Integer.parseInt(et_expectStep.getText().toString().trim()) + "";
        String height = Integer.parseInt(et_height.getText().toString().trim()) + "";
        String weight = Integer.parseInt(et_weight.getText().toString().trim()) + "";
        String sex = "";
        if (TextUtils.isEmpty(age) || TextUtils.isEmpty(expectStep) || TextUtils.isEmpty(height) || TextUtils.isEmpty(weight)) {
            ShowToastUtil.showToast(this,"输入不完整，请检查");
        } else {
            if (rb_nam.isChecked()) {
                sex = "1";
            } else sex = "0";
            SPUtils.put(this, "age", age);
            SPUtils.put(this, "expectStep", expectStep);

            try {
                x.getDb(SCApp.daoConfig).update(DayData.class, WhereBuilder.b("date", "=", GetDate.get()), new KeyValue("expect", expectStep));
            } catch (DbException e) {
                e.printStackTrace();
            }

            SPUtils.put(this, "height", height);
            SPUtils.put(this, "weight", weight);
            SPUtils.put(this, "sex", sex);
            ShowToastUtil.showToast(this,"保存成功");
            Intent intent = new Intent("update");
            sendBroadcast(intent);
            et_age.setText(age);
            et_expectStep.setText(expectStep);
            et_height.setText(height);
            et_weight.setText(weight);
        }
    }

    public void back(View view) {
        backVerify();
    }

    private void backVerify() {
        initData();
        if (et_weight.getText().toString().equals(weight) &&
                et_height.getText().toString().equals(height) &&
                et_expectStep.getText().toString().equals(expectStep) &&
                et_age.getText().toString().equals(age) &&
                (rb_nam.isChecked() ? "1" : "0").equals(sex)) {
            finish();

        } else {
            AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this);
            alertDialogBuilder.setTitle("注意").setMessage("内容已更改，是否保存？").setPositiveButton("是", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int which) {
                    saveData();
                    finish();
                }
            }).setNegativeButton("否", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int which) {
                }
            }).show();
        }
    }


    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        switch (keyCode) {
            case KeyEvent.KEYCODE_BACK:
            case KeyEvent.KEYCODE_ESCAPE:
                backVerify();

                break;

        }
        return true;
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        RefWatcher refWatcher = SCApp.getRefWatcher(this);
        refWatcher.watch(this);
        //AppManager.getAppManager().finishActivity();
    }
}
